package csel.controller.dispatchers;

import csel.controller.GameController;
import csel.model.GameCommand;
import csel.model.Model;

/**
 * Reasons for Existence: to organize and queue commands 
 * for the model created by listener threads</br>
 * Team Members Responsible</br>
 * Implementation: Nik, Tait</br>
 * Testing: Nik, Tait</br>
 *  
 * @author Nikolas Wolfe
 */
public class ModelDispatcher extends AbstractDispatcher
{
	private Model model;				
	
	// Constructor
	public ModelDispatcher(Model model) 
	{
		super();
		this.model = model;
	}

	/*
	 * This method calls take() on the blocking queue, which
	 * will wait() the queue object if no commands are available 
	 */
	public void dispatchCommand() throws InterruptedException
	{
		model.accept(getQueue().take());
		if(GameController.infoMode)System.out.println("[CONTROLLER] ModelDispatcher successfully completed " +
				"GameCommand dispatch.");
	}

	/*
	 * This method calls put() on the blocking queue, which will
	 * wait() on the queue object if no buffer space is available
	 */
	public void export(GameCommand gc) throws InterruptedException
	{
		getQueue().put(gc);
		if(GameController.infoMode)System.out.println("[CONTROLLER] ModelDispatcher sucessfully inserted GameCommand into " +
				"Command Queue.");
		synchronized( getWorker() ) {
			getWorker().notify();
		}
	}
	
	// TEST METHODS ONLY
}
